package com.ruoyi.project.pms.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.db.pms.domain.PmsCategory;
import com.ruoyi.db.pms.domain.query.PmsCategoryQueryVo;
import com.ruoyi.project.pms.service.PmsCategoryService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 商品分类Controller
 *
 * @author ruoyi
 * @date 2020-04-22
 */
@Api(tags="pms管理：商品分类管理")
@RestController
@RequestMapping("/pms/category")
public class PmsCategoryController extends BaseController
{
    @Autowired
    private PmsCategoryService pmsCategoryService;

    @ApiOperation("查询商品分类列表")
    @PreAuthorize("@ss.hasPermi('pms:category:list')")
    @GetMapping("/list")
    public AjaxResult list(PmsCategoryQueryVo pmsCategoryQueryVo)
    {
        List<PmsCategory> list = pmsCategoryService.selectPmsCategoryList(pmsCategoryQueryVo);
        return AjaxResult.success(list);
    }

    /**
     * @author 张涛
     * @return
     */
    @ApiOperation("查询所有一级分类及子分类")
    @RequestMapping(value = "/list/withChildren", method = RequestMethod.GET)
    @ResponseBody
    public AjaxResult listWithChildren() {
        List<PmsCategory> list = pmsCategoryService.listWithChildren();
        return AjaxResult.success(list);
    }

    @ApiOperation("导出商品分类列表")
    @Log(title = "商品分类", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('pms:category:export')")
    @GetMapping("/export")
    public AjaxResult export(PmsCategoryQueryVo pmsCategoryQueryVo)
    {
        List<PmsCategory> list = pmsCategoryService.selectPmsCategoryList(pmsCategoryQueryVo);
        ExcelUtil<PmsCategory> util = new ExcelUtil<PmsCategory>(PmsCategory.class);
        return util.exportExcel(list, "category");
    }

    @ApiOperation("获取商品分类详细信息")
    @Log(title = "商品分类", businessType = BusinessType.OTHER)
    @PreAuthorize("@ss.hasPermi('pms:category:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(pmsCategoryService.selectPmsCategoryById(id));
    }

    @ApiOperation("新增商品分类")
    @Log(title = "商品分类", businessType = BusinessType.INSERT)
    @PreAuthorize("@ss.hasPermi('pms:category:add')")
    @PostMapping
    public AjaxResult add(@RequestBody PmsCategory pmsCategory)
    {
        return toAjax(pmsCategoryService.insertPmsCategory(pmsCategory));
    }

    @ApiOperation("修改商品分类")
    @Log(title = "商品分类", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('pms:category:edit')")
    @PutMapping
    public AjaxResult edit(@RequestBody PmsCategory pmsCategory)
    {
        return toAjax(pmsCategoryService.updatePmsCategory(pmsCategory));
    }

    @ApiOperation("删除商品分类")
    @Log(title = "商品分类", businessType = BusinessType.DELETE)
    @PreAuthorize("@ss.hasPermi('pms:category:remove')")
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(pmsCategoryService.deletePmsCategoryByIds(ids));
    }
}
